//
//  SceneMgr.m
//  TestUsingCocos2dAndCocosBuilder
//
//  Created by Ronald Ram on 1/2/13.
//  Copyright 2013 Ronald Ram. All rights reserved.
//

#import "SceneMgr.h"
#import "MainMenu.h"

#import "CCBReader.h"


@interface SceneMgr()
- (void) debugData;
- (void) startUpData;

@end


@implementation SceneMgr

static SceneMgr *sharedScene_ = nil;
@synthesize current = current_, hud = hud_;

#pragma mark Init-Methods
+ (SceneMgr *) sharedScene
{
    if ( sharedScene_ == nil ) sharedScene_ = [SceneMgr node];
    return sharedScene_;
}

- (id) init
{
    if ( self = [super init] )
    {
//        [self debugData];
        [self startUpData];
    }
    return self;
}


#pragma mark Private-Method
- (void)startUpData
{
//    MainMenu *menu = [MainMenu node];
    CCLayer *menu = (CCLayer *) [CCBReader nodeGraphFromFile: @"MainMenu1.ccbi"];
    current_ = menu;
    [self addChild: menu];

}


- (void) debugData
{
//    CGSize size = [[CCDirector sharedDirector] winSize];
//
//    CCLayerColor *bg = [CCLayerColor layerWithColor: ccc4(70, 160, 200, 255) ];
//    [self addChild: bg];
//
//    CCLabelTTF *label = [CCLabelTTF labelWithString: @"Scene Manager" fontName: @"Copperplate" fontSize: 48];
//    label.position = ccp( size.width * 0.5, size.height - (size.height * 0.15) );
//    [self addChild: label];
    
    MainMenu *menu = [MainMenu node];
    current_ = menu;
    [self addChild: menu];
}


#pragma mark Public-Methods
- (void) switchLayer: (CCLayer *) layer
{
    CCLayer *tmp; BOOL canRemove = NO;
    
    if ( self.current != nil )
    {
        tmp = self.current; canRemove = YES;
    }

    self.current = layer;
    [self addChild: self.current];
    
    if ( canRemove ) [self removeChildByTag:tmp.tag cleanup:YES]; //[self removeChild: tmp cleanup:YES];
}


- (void) dealloc
{
//    [self removeAllChildrenWithCleanup: YES];
    self.current = nil;
    self.hud = nil;
    
    [super dealloc];
}

@end
